1
'****************************** Module Header ******************************\
2 ' Module Name: ConfigurationEncryption.aspx.vb
3 ' Project: VBASPNETEncryptAndDecryptConfiguration
4 ' Copyright (c) Microsoft Corporation.
6 ' This sample shows how to use RSA encryption algorithm API to encrypt and decrypt
7 'configuration section in order to protect the sensitive information from interception
8 'or hijack in ASP.NET web application.
10 ' The project contains two snippets. The First One demonstrates how to use RSA provider
11 'and RSA container to encrypt and decrypt some words or values in web application.
12 'the purpose of first snippet is to let us know the overview of RSA mechanism.
13 'Second one shows how to use RSA configuration provider to encrypt and decrypt
14 'configuration section in web.config.
16 ' This source is subject to the Microsoft Public License.
17 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
18 ' All other rights reserved.
20 '\***************************************************************************/
22 Imports System
.Web
.Configuration
24 Public Class ConfigurationEncryption_aspx
25 Inherits System
.Web
.UI
.Page
27 Private Const provider
As String = "RSAProtectedConfigurationProvider"
28 'Use RSA Provider to encrypt configuration sections
30 Protected
Sub Page_Load(ByVal sender
As Object, ByVal e
As System
.EventArgs
) Handles
Me.Load
34 Protected
Sub btnEncrypt_Click(ByVal sender
As Object, ByVal e
As EventArgs
) Handles btnEncrypt
.Click
35 If String.IsNullOrEmpty(Me.ddlSection
.SelectedValue
) Then
36 Response
.Write("please select a configuration section")
40 Dim sectionString
As String = Me.ddlSection
.SelectedValue
42 Dim config
As System
.Configuration
.Configuration
= WebConfigurationManager
.OpenWebConfiguration(Request
.ApplicationPath
)
43 Dim section
As ConfigurationSection
= config
.GetSection(sectionString
)
44 If section IsNot
Nothing Then
45 section
.SectionInformation
.ProtectSection(provider
)
47 Response
.Write("encrypt successed, please check the configuration file.")
51 Protected
Sub btnDecrypt_Click(ByVal sender
As Object, ByVal e
As EventArgs
) Handles btnDecrypt
.Click
52 Dim sectionString
As String = Me.ddlSection
.SelectedValue
54 Dim config
As System
.Configuration
.Configuration
= WebConfigurationManager
.OpenWebConfiguration(Request
.ApplicationPath
)
55 Dim section
As ConfigurationSection
= config
.GetSection(sectionString
)
56 If section IsNot
Nothing AndAlso section
.SectionInformation
.IsProtected
Then
57 section
.SectionInformation
.UnprotectSection()
59 Response
.Write("decrypt success, please check the configuration file.")